package com.itheima.goods.controller;

import com.itheima.goods.pojo.Category;
import com.itheima.goods.service.CategoryService;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/category")

public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/findAll")
    public Result findAll() {
        return categoryService.findAll();
        //category/findByParentId?parentId=0
    }

    @GetMapping("/findByParentId")
    public Result findByParentId(@RequestParam("parentId") Integer parentId) {


        return categoryService.findByParentId(parentId);
    }

    @GetMapping("/addCategory")
    public Result addCategory(@RequestBody Category category) {
        return this.categoryService.addCategory(category);
    }

    @GetMapping("/findPage")
    public PageResult findPage(@RequestParam("currentPage") Integer currentPage,
                               @RequestParam("pageSize") Integer pageSize,
                               @RequestParam("queryString") Integer queryString) {
        return this.categoryService.findPage(currentPage, pageSize, queryString);
    }

    @GetMapping
    public Result findById(@RequestParam("id") Integer id) {
        return this.categoryService.findById(id);
    }

    @PutMapping
    public Result updateCategory(Integer id, Category category) {
        return this.categoryService.updateCategory(id, category);
    }

    @DeleteMapping
    public Result deleteCategory(Integer id) {
        return this.categoryService.deleteCategory(id);
    }
}
